package com.atguigu.jxc.controller;

import com.atguigu.jxc.annotation.LogToDb;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 分页查询供应商
     *
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("list")
    @ResponseBody
    public Map<String, Object> list(
            Integer page,
            Integer rows,
            String supplierName
    ) {
        return supplierService.listSupplier(page, rows, supplierName);
    }

    /**
     * 供应商修改
     * @param supplierId
     * @param supplier
     * @return
     */
    @ResponseBody
    @PostMapping("save")
    public ServiceVO save(Integer supplierId, Supplier supplier) {
        Supplier supplierForSave = new Supplier();
        BeanUtils.copyProperties(supplier, supplierForSave);
        supplier.setSupplierId(supplierId);
        supplierService.save(supplierForSave);
        return new ServiceVO(100, "请求成功", null);
    }

    @ResponseBody
    @PostMapping("delete")
    public ServiceVO delete(String[] ids){
        supplierService.deleteBatch(ids);
        return new ServiceVO(100, "请求成功", null);
    }
}
